package com.spa.domain.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.ResourceAddCommand;
import com.spa.application.command.admin.ResourceEditCommand;
import com.spa.application.query.admin.ResourceQuery;
import com.spa.domain.entity.Resource;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.mapper.ResourceMapStruct;
import com.spa.infrastructure.repository.po.ResourcePO;
import com.spa.infrastructure.repository.service.ResourceRepositoryService;
import com.spa.infrastructure.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资源服务.
 *
 * @author liangjun
 */
@Service
public class ResourceService {

    @Autowired
    private ResourceRepositoryService resourceRepositoryService;

    /**
     * 分页查询.
     *
     * @return 资源
     */
    public Page<Resource> page(ResourceQuery query) {
        return ResourceMapStruct.INSTANCE.toResourcePage(resourceRepositoryService.page(query));
    }

    /**
     * 查询资源列表.
     *
     * @return 资源列表
     */
    public List<Resource> list(ResourceQuery query) {
        return ResourceMapStruct.INSTANCE.toResourceList(resourceRepositoryService.queryResources(query));
    }

    /**
     * 新增一个资源.
     */
    public void add(ResourceAddCommand command) {
        List<Resource> resourceList = listByParentId(command.getParentId());
        if (CollUtil.isNotEmpty(resourceList)) {
            verifyNameRepetition(command.getParentId(), command.getResourceName());
        }
        resourceRepositoryService.add(ResourceMapStruct.INSTANCE.addCommandToResourcePO(command));
    }

    /**
     * 修改一个资源.
     */
    public void edit(ResourceEditCommand command) {

        ResourcePO po = resourceRepositoryService.getById(command.getId());
        if (StringUtils.isNotEmpty(po.getResourceName()) && !po.getResourceName().equals(command.getResourceName())) {
            verifyNameRepetition(command.getParentId(), command.getResourceName());
        }

        resourceRepositoryService.edit(ResourceMapStruct.INSTANCE.editCommandToResourcePO(command));
    }

    /**
     * 删除一个资源.
     */
    public boolean delById(Long id) {
        if (CollUtil.isNotEmpty(listByParentId(id))) {
            throw new SpaException("请先删除子菜单");
        }
        return resourceRepositoryService.removeById(id);
    }

    /**
     * 启用/禁用.
     */
    public boolean enableDisabled(Long id) {
        ResourcePO po = resourceRepositoryService.getById(id);
        if (BeanUtil.isNotEmpty(po)) {
            if(WhetherTypeEnum.$0.getCode().equals(po.getState())){
                po.setState(WhetherTypeEnum.$1.getCode());
            }else {
                po.setState(WhetherTypeEnum.$0.getCode());
            }
            resourceRepositoryService.edit(po);
        }
        return false;
    }

    /**
     * 获取资源列表
     *
     * @param parentId 上级ID
     * @return 资源列表
     */
    public List<Resource> listByParentId(Long parentId) {
        ResourceQuery query = new ResourceQuery();
        query.setParentId(parentId);
        return ResourceMapStruct.INSTANCE.toResourceList(resourceRepositoryService.queryResources(query));
    }

    /**
     * 验证名称重复
     */
    public void verifyNameRepetition(Long parentId, String name) {
        List<Resource> resourceList = listByParentId(parentId);
        if (CollUtil.isNotEmpty(resourceList)) {
            long i = resourceList.stream().filter(a -> a.getResourceName().equals(name)).count();
            if (i > 0) {
                throw new SpaException("同一级别下菜单名称不能重复");
            }
        }
    }
}
